(function() {
  document.addEventListener('click',function (e) {
    var parent=$(e.target).parents('.search-com');
    if(parent.length===0){
      $('.search-drop').hide()
    }
  })
})()
var app = new Vue({
  el: ".appheader",
  data() {
    return {
      searchnum: 1,
      showdrop: false,
      imgstr:'baidu',
      searchkey: "",
      ischeck: false,
      ischeckk: false,
      Switch: 1,
      emailnumber: "", //登录邮箱
      password: "", //登录密码
      inputtext: {
        //注册
        emailnumberr: "", //邮箱
        password1: "", //一次密码
        password2: "" //二次密码
      },
      istrue: false,
      istruee: true,
      showprotocol: false,
      theuser: "",
      information: "",
      inputtext: {
        email: "", //邮箱
        password: "", //一次密码
        rpassword: "" //二次密码
      },
      isfalse: false,
      emailcode: "发送验证码",
      checkCode: "",
      passwordprompt: ""
    };
  },
  created() {
    if (localStorage.getItem("ID")) {
      this.istruee = false;
      var userbasicinfo = false;
    }
    this.emailnumber = localStorage.getItem("emailnumber");
    // this.password = localStorage.getItem("password");
    if(localStorage.getItem("emailnumber")){//邮箱请求密码
      // let params = {
      //   email: localStorage.getItem("emailnumber")
      // };
      axios.post("/yesUser/GetPassword", {email:this.emailnumber}).then(res => {
        if (res.data.code === 200) {
          this.password = res.data.data
        }else if (res.data.code == -10002) {
        }
      });
      
    }
  },
  methods: {
    showdropbox(){
      this.showdrop = !this.showdrop
      if(this.showdrop){
        $('.search-drop').show()
      }else{
        $('.search-drop').hide()
      }
    },
    chiosesearch(val){
      this.searchnum = val
      let i = this.searchnum
      if(i == 1) {this.imgstr = 'baidu'}
      else if(i == 2) {this.imgstr = 'bing'}
      else if(i == 3) {this.imgstr = 'sogo'}
      this.showdrop = false
      $('.search-drop').hide()
    },
    submitt() {
      //重置密码
      if (this.inputtext.email == "") {
        this.inputtext.email = "请输入邮箱";
      } else if (this.checkCode == "") {
        this.checkCode = "验证码不能为空";
      } else if (this.checkCode.length > 6) {
        this.checkCode = "验证码长度不正确";
      } else if (this.inputtext.password == "") {
        this.inputtext.password = "请输入密码";
      } else if (this.inputtext.rpassword == "") {
        this.inputtext.rpassword = "请输入新密码";
      } else if (this.inputtext.password != this.inputtext.rpassword) {
        this.passwordprompt = "两次密码输入不一致";
      } else {
        let parm = {
          email: this.inputtext.email,
          password: this.inputtext.password,
          checkCode: parseFloat(this.checkCode)
        };
        axios.post("/yesUser/forgetPassword", parm).then(res => {
          if (res.data.code == 200) {
            location.href = "/";
          } else if (res.data.code == -10003) {
            this.checkCode = "验证码错误";
          } else if (res.data.code == -10007) {
            this.checkCode = "验证码过期";
          }
        });
      }
    },
    sendcode() {
      //验证码
      let that = this;
      if (this.inputtext.email == "") {
        this.inputtext.email = "请输入邮箱";
        return;
      } else {
        let params = {
          sendTo: this.inputtext.email
        };
        if (this.emailcode == "发送验证码" || this.emailcode == "重新发送") {
          axios
            .post("/yesUser/eamilcheck", { email: this.inputtext.email })
            .then(res => {
              if (res.data.code == 200) {
                this.inputtext.email = "邮箱不存在";
              } else if (res.data.code == -10002) {
                axios.post("/comm/mail/send", params).then(res => {
                  if (res.data.code == 200) {
                    let time = 60;
                    let timer = setInterval(function() {
                      time -= 1;
                      that.emailcode = time;
                      if (time <= 0) {
                        clearInterval(timer);
                        that.emailcode = "重新发送";
                      }
                    }, 1000);
                  } else if (res.data.code == -10003) {
                    this.sendcode = "邮件发送失败";
                  } else if (res.data.code == -10008) {
                    this.sendcode = "邮件发送失败";
                  }
                });
              }
            });
        }
      }
    },
    login() {
      //请求登录
      // let that=this
      this.istrue = true;
      if (this.emailnumber == "" || this.password == "") {
        var a =
          '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", a);
        var b = '<div class="maskLayer"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", b);
        document.getElementsByClassName("closeinfo")[0].onclick = function() {
          var c = document.getElementsByClassName("maskLayer")[0]; //移除元素
          var d = document.getElementsByClassName("errorMessage")[0]; //移除元素
          document.querySelector(".appheader").removeChild(c);
          document.querySelector(".appheader").removeChild(d);
        };
        document.getElementsByClassName("errorMessageTop")[0].innerHTML =
          "账号或密码不能为空";
        document.getElementsByClassName("errorMessageBottom")[0].innerHTML =
          "账号或密码不能为空";
        this.istrue = false;
        return;
      } else {
        axios
          .post("/yesUser/eamilcheck", { email: this.emailnumber })
          .then(res => {
            if (res.data.code == 200) {
              var a =
                '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>';
              document
                .querySelector(".appheader")
                .insertAdjacentHTML("afterBegin", a);
              var b = '<div class="maskLayer"></div>';
              document
                .querySelector(".appheader")
                .insertAdjacentHTML("afterBegin", b);
              document.getElementsByClassName(
                "closeinfo"
              )[0].onclick = function() {
                var c = document.getElementsByClassName("maskLayer")[0]; //移除元素
                var d = document.getElementsByClassName("errorMessage")[0]; //移除元素
                document.querySelector(".appheader").removeChild(c);
                document.querySelector(".appheader").removeChild(d);
              };
              document.getElementsByClassName("errorMessageTop")[0].innerHTML =
                "邮箱不存在";
              document.getElementsByClassName(
                "errorMessageBottom"
              )[0].innerHTML =
                "邮箱不存在";
              this.istrue = false;
              return;
            } else if (res.data.code == -10002) {
              let params = {
                email: this.emailnumber,
                password: this.password,
                isRemember:this.ischeck
              };
              axios.post("/yesUser/signin", params).then(res => {
                if (res.data.code == 200) {
                  if (!this.ischeck) {
                    localStorage.removeItem("emailnumber");
                    localStorage.removeItem("password");
                    let data = res.data.data;
                    localStorage.setItem("Email", data.email);
                    localStorage.setItem("user", data.nickname);
                    localStorage.setItem("ID", data.userId);
                    sessionStorage.setItem("ID", data.userId);
                    localStorage.setItem("mytoken", data.token);
                    location.href = '/'
                    this.hideMask();
                  } else {
                    // document.cookie="password="+this.password;
                    // document.cookie="emailnumber="+this.emailnumber;
                    localStorage.setItem("emailnumber", this.emailnumber, 7); //保存帐号到cookie，有效期7天
                    // localStorage.setItem("password", this.password, 7);
                    let data = res.data.data;
                    localStorage.setItem("Email", data.email);
                    localStorage.setItem("user", data.nickname);
                    localStorage.setItem("ID", data.userId);
                    localStorage.setItem("mytoken", data.token);
                    sessionStorage.setItem("ID", data.userId);
                    location.href = '/'
                    this.hideMask();
                  }
                  // let data = JSON.parse(res.data.data)
                  // localStorage.setItem('Email', data.email)
                  // localStorage.setItem('user', data.nickname)
                  // localStorage.setItem('ID', data.userId)
                  // localStorage.setItem('mytoken', data.token)
                  // location.href = '/'  uid:localStorage.getItem('ID'),
                } else if (res.data.code == -10005) {
                  var a =
                    '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>';
                  document
                    .querySelector(".appheader")
                    .insertAdjacentHTML("afterBegin", a);
                  var b = '<div class="maskLayer"></div>';
                  document
                    .querySelector(".appheader")
                    .insertAdjacentHTML("afterBegin", b);
                  document.getElementsByClassName(
                    "closeinfo"
                  )[0].onclick = function() {
                    var c = document.getElementsByClassName("maskLayer")[0]; //移除元素
                    var d = document.getElementsByClassName("errorMessage")[0]; //移除元素
                    document.querySelector(".appheader").removeChild(c);
                    document.querySelector(".appheader").removeChild(d);
                  };
                  document.getElementsByClassName(
                    "errorMessageTop"
                  )[0].innerHTML =
                    "您输入的密码有误";
                  document.getElementsByClassName(
                    "errorMessageBottom"
                  )[0].innerHTML =
                    "您输入的密码有误";
                }
                this.istrue = false;
                this.istruee = false;
              });
            }
          });
      }
    },
    signout() {
      //退出登录
      axios.post("/yesUser/usersignout", {}).then(res => {
        if (res.data.code === 200) {
          location.href = "/";
          // localStorage.clear()
          // sessionStorage.clear()
          localStorage.removeItem("Email");
          localStorage.removeItem("ID");
          localStorage.removeItem("user");
          localStorage.removeItem("mytoken");
          sessionStorage.removeItem("ID")
        }
      });
    },
    submit() {
      //请求注册
      var reg = /^\+?[1-9]\d*$/;
      let regValid = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
      if (!this.ischeckk) {
        var a =
          '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", a);
        var b = '<div class="maskLayer"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", b);
        document.getElementsByClassName("closeinfo")[0].onclick = function() {
          var c = document.getElementsByClassName("maskLayer")[0]; //移除元素
          var d = document.getElementsByClassName("errorMessage")[0]; //移除元素
          document.querySelector(".appheader").removeChild(c);
          document.querySelector(".appheader").removeChild(d);
        };
        document.getElementsByClassName("errorMessageTop")[0].innerHTML =
          "请勾选协议";
        document.getElementsByClassName("errorMessageBottom")[0].innerHTML =
          "请勾选协议";
        return;
      } else if (
        this.inputtext.emailnumberr == undefined ||
        this.inputtext.password1 == undefined ||
        this.inputtext.password2 == undefined
      ) {
        var a =
          '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", a);
        var b = '<div class="maskLayer"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", b);
        document.getElementsByClassName("closeinfo")[0].onclick = function() {
          var c = document.getElementsByClassName("maskLayer")[0]; //移除元素
          var d = document.getElementsByClassName("errorMessage")[0]; //移除元素
          document.querySelector(".appheader").removeChild(c);
          document.querySelector(".appheader").removeChild(d);
        };
        document.getElementsByClassName("errorMessageTop")[0].innerHTML =
          "信息不完整";
        document.getElementsByClassName("errorMessageBottom")[0].innerHTML =
          "信息不完整";
        return;
      }
      if (!regValid.test(this.inputtext.emailnumberr)) {
        var a =
          '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", a);
        var b = '<div class="maskLayer"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", b);
        document.getElementsByClassName("closeinfo")[0].onclick = function() {
          var c = document.getElementsByClassName("maskLayer")[0]; //移除元素
          var d = document.getElementsByClassName("errorMessage")[0]; //移除元素
          document.querySelector(".appheader").removeChild(c);
          document.querySelector(".appheader").removeChild(d);
        };
        document.getElementsByClassName("errorMessageTop")[0].innerHTML =
          "邮箱不正确";
        document.getElementsByClassName("errorMessageBottom")[0].innerHTML =
          "邮箱不正确";
        return;
      }
      if (this.inputtext.password1 != this.inputtext.password2) {
        var a =
          '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", a);
        var b = '<div class="maskLayer"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", b);
        document.getElementsByClassName("closeinfo")[0].onclick = function() {
          var c = document.getElementsByClassName("maskLayer")[0]; //移除元素
          var d = document.getElementsByClassName("errorMessage")[0]; //移除元素
          document.querySelector(".appheader").removeChild(c);
          document.querySelector(".appheader").removeChild(d);
        };
        document.getElementsByClassName("errorMessageTop")[0].innerHTML =
          "密码输入不一致";
        document.getElementsByClassName("errorMessageBottom")[0].innerHTML =
          "密码输入不一致";
        return;
      }
      if (
        this.inputtext.password1.length < 6 ||
        this.inputtext.password2.length < 6
      ) {
        var a =
          '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", a);
        var b = '<div class="maskLayer"></div>';
        document
          .querySelector(".appheader")
          .insertAdjacentHTML("afterBegin", b);
        document.getElementsByClassName("closeinfo")[0].onclick = function() {
          var c = document.getElementsByClassName("maskLayer")[0]; //移除元素
          var d = document.getElementsByClassName("errorMessage")[0]; //移除元素
          document.querySelector(".appheader").removeChild(c);
          document.querySelector(".appheader").removeChild(d);
        };
        document.getElementsByClassName("errorMessageTop")[0].innerHTML =
          "密码长度小于6位";
        document.getElementsByClassName("errorMessageBottom")[0].innerHTML =
          "密码长度小于6位";
        return;
      }
      let params = {
        email: this.inputtext.emailnumberr,
        password: this.inputtext.password1
      };
      axios.post("/yesUser/register", params).then(res => {
        if (res.data.code == -10002) {
          // this.hideMask()
          var a =
            '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>';
          document
            .querySelector(".appheader")
            .insertAdjacentHTML("afterBegin", a);
          var b = '<div class="maskLayer"></div>';
          document
            .querySelector(".appheader")
            .insertAdjacentHTML("afterBegin", b);
          document.getElementsByClassName("closeinfo")[0].onclick = function() {
            var c = document.getElementsByClassName("maskLayer")[0]; //移除元素
            var d = document.getElementsByClassName("errorMessage")[0]; //移除元素
            document.querySelector(".appheader").removeChild(c);
            document.querySelector(".appheader").removeChild(d);
          };
          document.getElementsByClassName("errorMessageTop")[0].innerHTML =
            "邮箱以存在";
          document.getElementsByClassName("errorMessageBottom")[0].innerHTML =
            "邮箱以存在";
          this.Switch = 0;
        } else if (res.data.code == 200) {
          this.hideMask();
          var a =
            '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>';
          document
            .querySelector(".appheader")
            .insertAdjacentHTML("afterBegin", a);
          var b = '<div class="maskLayer"></div>';
          document
            .querySelector(".appheader")
            .insertAdjacentHTML("afterBegin", b);
          document.getElementsByClassName("closeinfo")[0].onclick = function() {
            var c = document.getElementsByClassName("maskLayer")[0]; //移除元素
            var d = document.getElementsByClassName("errorMessage")[0]; //移除元素
            document.querySelector(".appheader").removeChild(c);
            document.querySelector(".appheader").removeChild(d);
          };
          document.getElementsByClassName("errorMessageTop")[0].innerHTML =
            "注册成功";
          document.getElementsByClassName("errorMessageBottom")[0].innerHTML =
            "注册成功";
          this.Switch = 0;
        } else if (res.data.code == -10003) {
          var a =
            '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>';
          document
            .querySelector(".appheader")
            .insertAdjacentHTML("afterBegin", a);
          var b = '<div class="maskLayer"></div>';
          document
            .querySelector(".appheader")
            .insertAdjacentHTML("afterBegin", b);
          document.getElementsByClassName("closeinfo")[0].onclick = function() {
            var c = document.getElementsByClassName("maskLayer")[0]; //移除元素
            var d = document.getElementsByClassName("errorMessage")[0]; //移除元素
            document.querySelector(".appheader").removeChild(c);
            document.querySelector(".appheader").removeChild(d);
          };
          document.getElementsByClassName("errorMessageTop")[0].innerHTML =
            "注册有误,请重新注册";
          document.getElementsByClassName("errorMessageBottom")[0].innerHTML =
            "注册有误,请重新注册";
        }
      });
    },
    Switcch(num) {
      //登录注册切换
      this.Switch = num;
    },
    CheckAgreement() {
      //记住密码
      this.ischeck = !this.ischeck;
    },
    CheckAgreementt() {
      //勾选协议
      this.ischeckk = !this.ischeckk;
    },
    mounted: function() {
      this.showMask();
      this.hideMask();
    },
    quicksearch: function(e) {
      //回车及Enter百度搜索
      if (e.keyCode == 13 || e.keyCode == 108) {
        this.search();
      }
    },
    quicklogin: function(e) {
      //回车及Enter登录
      if (e.keyCode == 13 || e.keyCode == 108) {
        this.login();
      }
    },
    quicksubmit: function(e) {
      //回车及Enter注册
      if (e.keyCode == 13 || e.keyCode == 108) {
        this.submit();
      }
    },
    search() {
      if (this.searchkey == "") {
        return;
      }
      if(this.searchnum == 1){
        window.open(`https://www.baidu.com/s?wd=${this.searchkey}`, '') 
      }else if(this.searchnum == 2){
        window.open(`https://cn.bing.com/search?q=${this.searchkey}`,'')
      }else if(this.searchnum == 3){
        window.open(`https://www.sogou.com/web?query=${this.searchkey}`,'')
      }
    },
    showMask(index) {
      //打开遮罩层
      $("#mask").css("height", $(document).height());
      $("#close").css("display", "block");
      // $("#mask").css("width", "100%");
      $("#mask").show();
      //使遮罩下方内容不可滚动
      $("body").css("overflow", "hidden");
      this.Switch = index;
    },
    hideMask() {
      //关闭遮罩层
      $("#mask").hide();
      $("#close").css("display", "none");
      $("body").css("overflow", "auto");
    },
    SetHome(refname, vrl) {
      let obj = this.$refs[refname];
      try {
        obj.style.behavior = "url(#default#homepage)";
        obj.setHomePage(vrl);
      } catch (e) {
        if (window.netscape) {
          try {
            netscape.security.PrivilegeManager.enablePrivilege(
              "UniversalXPConnect"
            );
          } catch (e) {
            alert(
              "此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。"
            );
          }
          var prefs = Components.classes[
            "@mozilla.org/preferences-service;1"
          ].getService(Components.interfaces.nsIPrefBranch);
          prefs.setCharPref("browser.startup.homepage", vrl);
        } else {
          alert(
            "您的浏览器不支持，请按照下面步骤操作：1.打开浏览器设置。2.点击设置网页。3.输入：" +
              vrl +
              "点击确定。"
          );
        }
      }
    }
  }
});
